package closable;

/**
 * jdk7红AutoCloseable用例
 */
public class ResourceTests {

    public static void main(String[] args) {
        autoCLose();
    }

    private static void autoCLose() {
        try (Resource resource = new Resource(0x01)) {
            int id = resource.getId();
            //int v = 1 / 0;
            System.out.println("id is:" + id);
        } catch (Exception e) {
            System.out.println("catch:" + e.toString());
        } finally {
            System.out.println("finally()");
        }
    }


    private static class Resource implements AutoCloseable {

        private int id;

        public Resource(int id) {
            this.id = id;
        }

        public int getId() {
            return id;
        }

        @Override
        public void close() throws Exception {
            System.out.println("resource instance has been closed");
        }
    }
}